package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"permission:view", "权限列表"}, logical = Logical.OR)
    public String view() {

        return "permission/permission";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(QueryObject qo) {
        return permissionService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Permission> selectAll() {
        return permissionService.selectAll();
    }

    @ResponseBody
    @RequestMapping("getPermissionByRoleId")
    public List<Permission> getPermissionByRoleId(Long roleId) {
        return permissionService.getPermissionByRoleId(roleId);
    }

    @ResponseBody
    @RequestMapping("loadPermission")
    @RequiresPermissions(value = {"permission:loadPermission", "权限加载"}, logical = Logical.OR)
    public JSONResult loadPermission() {
        try {
            permissionService.loadPermission();
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("加载失败");
        }
    }
}
